from datetime import datetime
from time import time
from django.http import HttpResponse
from django.shortcuts import render
import os
from htmltool import DiffType
from .forms import AnalyseA2lS19ChooseForm,AnalyseA2lS19VarForm
from . import checkfile
from .models import A2lIncaObj
from django.db.models import DateField


def hello(request):
    return render(request, r'rob.html', {'f':AnalyseA2lS19ChooseForm(),'hello':'XP HH HelloWorld','l1':[1,2,3,6,7,8]})


anavar = checkfile.CheckFile()


def analyse(request):
    if request.method == "POST":
        form = AnalyseA2lS19ChooseForm(request.POST, request.FILES)
        tmpdir = f'SH_{datetime.today().strftime("%Y%m%d_%H%M")}_{hash(time())>>11}'
        os.mkdir(f"files/{tmpdir}")
        for k, v in form.files.items():
            with open(f"files/{tmpdir}/{v.name}", "wb+") as destination:
                for chunk in v.chunks():
                    destination.write(chunk)
        anavar.__init__()
        anavar.old_file = [
            f'files/{tmpdir}/{form.files["old_a2l_file"].name}',
            f'files/{tmpdir}/{form.files["old_s19_file"].name}',
        ]
        anavar.new_file = [
            f'files/{tmpdir}/{form.files["new_a2l_file"].name}',
            f'files/{tmpdir}/{form.files["new_s19_file"].name}',
        ]
        A2lIncaObj.objects.all().delete()
        obj = A2lIncaObj(
            a2l_name_old="",
            a2l_name_new="",
            inca_fd_name="",
            old_file_a2l=anavar.old_file[0],
            old_file_s19=anavar.old_file[1],
            new_file_a2l=anavar.new_file[0],
            new_file_s19=anavar.new_file[1],
        )
        obj.save()
        return render(request, r"analyse.html", {"f": AnalyseA2lS19VarForm()})


def analysevar(request):
    if request.method == "POST":
        print("aaaaaaaa")
        form = AnalyseA2lS19VarForm(request.POST)
        text = form.data["vartext"]
        l = [x.strip() for x in text.split() if x.strip()]
        if not anavar.new_file[0]:
            obj = A2lIncaObj.objects.get(date=DateField(auto_now_add=True))
            if not obj:
                return
            anavar.a2l_name[0] = obj.a2l_name_old
            anavar.a2l_name[1] = obj.a2l_name_new
            anavar.inca_fd_name = obj.inca_fd_name
            anavar.old_file[0] = obj.old_file_a2l
            anavar.old_file[1] = obj.old_file_s19
            anavar.new_file[0] = obj.new_file_a2l
            anavar.new_file[1] = obj.new_file_s19
            obj.delete()
        data = anavar.diff_a2l(l, True)
        obj = A2lIncaObj(
            a2l_name_old=anavar.a2l_name[0],
            a2l_name_new=anavar.a2l_name[1],
            inca_fd_name=anavar.inca_fd_name,
            old_file_a2l=anavar.old_file[0],
            old_file_s19=anavar.old_file[1],
            new_file_a2l=anavar.new_file[0],
            new_file_s19=anavar.new_file[1],
        )
        obj.save()
        return render(
            request,
            "analysevar.html",
            {"data": data, "DELETE": DiffType.DELETE, "NOFOUND": DiffType.NOFOUND},
        )
